package com.ljwm.security;

import com.google.common.collect.Maps;
import com.ljwm.bootbase.security.IJwtAndSecurityAble;
import com.ljwm.bootbase.security.LoginInfoHolder;
import com.ljwm.entity.User;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.experimental.Accessors;
import org.springframework.security.core.GrantedAuthority;

import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import java.util.Map;
import java.util.Objects;

/**
 * BootBaseDemo Created by yunqisong on 2018-05-22.
 * FOR:
 */
@Data
@Accessors(chain = true)
@AllArgsConstructor
@SuppressWarnings("ALL")
public class JwtUser implements IJwtAndSecurityAble {


    private User user;


    @Override
    public <T extends Serializable> T getId() {
        return (T) user.getId();
    }

    @Override
    public Date getLastModifyPasswordTime() {
        return user.getLastmodifyPassTime();
    }

    @Override
    public String getLoginType() {
        return LoginInfoHolder.getLoginType();
    }

    @Override
    public Map<String, String> extInfo() {
        return LoginInfoHolder.getExtInfo() == null ? Maps.newConcurrentMap() : LoginInfoHolder.getExtInfo();
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        //TODO: 权限
        return null;
    }

    @Override
    public String getPassword() {

        return user.getPassword();
    }

    @Override
    public String getUsername() {

        return user.getUsername();
    }

    @Override
    public boolean isAccountNonExpired() {
        return isEnabled();
    }

    @Override
    public boolean isAccountNonLocked() {
        return isEnabled();
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return isEnabled();
    }

    @Override
    public boolean isEnabled() {
        return Objects.equals(user.getStatus(), 0);
    }
}
